package com.etongwl.socketlibrary.library2;


/**
 * Created by jason on 16/6/27.
 * 消息处理核心
 */
public class LooperCore {
    //消息队列
    public static MessageQueueCore<MessageCore> mQueue = new MessageQueueCore<MessageCore>();;

    private static LooperCore looper;

    // 每个线程中的Looper对象其实是一个ThreadLocal，即线程本地存储(TLS)对象
    private static final ThreadLocal sThreadLocal = new ThreadLocal();

    // 当前线程
    private Thread mThread;
    //退出 true退出
    private static boolean quit =false;

    /**
     * 构造方法
     */
    private LooperCore() {
//        mQueue = new MessageQueueCore<MessageCore>();
        mThread = Thread.currentThread();
    }

    /**
     * 准备循环队列
     */
    public static void prepare() {
        quit = false;
        if (sThreadLocal.get() != null) {
            // 试图在有Looper的线程中再次创建Looper将抛出异常
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        looper =new LooperCore();
        sThreadLocal.set(looper);
    }

    /**
     * 循环队列开始
     *
     * @throws InterruptedException
     */
    public static void loop() {
            LooperCore me = myLooper();  //得到当前线程Looper
            MessageQueueCore<MessageCore> queue = me.mQueue;  //得到当前looper的MQ
            // 开始循环
            while (true&&!quit) {
                    System.out.println("队列循环开始 阻塞"+me.mQueue.hashCode());
                    MessageCore msg = queue.getMessage(); // 取出message
                    System.out.println("队列循环中...");
                    if (msg != null) {
                        if (msg.target == null) {
                            System.out.println("循环队列停止");
                            // message没有target为结束信号，退出循环
                            return;
                        }
                        // 非常重要！将真正的处理工作交给message的target，即后面要讲的handler
                        msg.target.dispatchMessage(msg);
                        // 回收message资源
                        msg.recycle();
                    }
            }
    }

    /**
     * 获取当前线程
     *
     * @return
     */
    public Thread getThread() {
        return mThread;
    }


    /**
     * 获取Looper 当前线程中的looper
     *
     * @return
     */
    public static final LooperCore myLooper() {
        // 在任意线程调用Looper.myLooper()返回的都是那个线程的looper
        return (LooperCore) sThreadLocal.get();
    }

    /**
     * 获取全局的looper
     * @return
     */
    public static final  LooperCore getLooper(){
        if (looper ==null){
            looper =new LooperCore();
            sThreadLocal.set(looper);
        }
        return looper;
    }

    /**
     * 退出
     *
     * @throws InterruptedException
     */
    public void quit() {
        try {
            // 创建一个空的message，它的target为NULL，表示结束循环消息
            MessageCore msg = MessageCore.obtain(null);
//            System.out.println("quit run");
            // 发出消息
            mQueue.setMessage(msg);
        } catch (InterruptedException e) {
            quit=true;
            e.printStackTrace();
        }
    }
}
